capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local log "";
local do "";
local crime "";
local data "";
local output "";
local zip "" ;
local earn "";
local parearn "";
local paroth "";

/* 
Main Prep 3, variables defined by calendar time
*/


local exp "prworard";
local range "1997(3)1997" ; /* "1997(3)2000" */
local firstyr 1997 ;	/* Kids turn 18 in 1996 */
local lastyr 2019 ;		/* Last year of crime data */
local lastearnyr 2019 ;

/* ******************* */
/* **** DATA PREP **** */
/* ******************* */

local first 1996 ;
local last 2004 ;
local per_range "20" ; 
local thresh 0.8 ; 	
include "`do'\crime_statecntyselect.do" ;

use "`crime'\prep2\crime_prwora_remerge.dta", clear;

/* Diagnosis */
tab n_diag1cat, m;
gen byte n_diag1_infec=(n_diag1cat==1);
gen byte n_diag1_neo=(n_diag1cat==2);
gen byte n_diag1_endo=(n_diag1cat==3);
gen byte n_diag1_blood=(n_diag1cat==4);
gen byte n_diag1_mental=(n_diag1cat==5);
gen byte n_diag1_nerv=(n_diag1cat==6);
gen byte n_diag1_sense=(n_diag1cat==7);
gen byte n_diag1_circ=(n_diag1cat==8);
gen byte n_diag1_resp=(n_diag1cat==9);
gen byte n_diag1_dig=(n_diag1cat==10);
gen byte n_diag1_gu=(n_diag1cat==11);
gen byte n_diag1_preg=(n_diag1cat==12);
gen byte n_diag1_skin=(n_diag1cat==13);
gen byte n_diag1_musc=(n_diag1cat==14);
gen byte n_diag1_cong=(n_diag1cat==15);
gen byte n_diag1_nat=(n_diag1cat==16);
gen byte n_diag1_ill=(n_diag1cat==17);
gen byte n_diag1_inj=(n_diag1cat==18);
gen byte n_diag1_none=(n_diag1cat==.);
replace n_diag1cat = 999 if n_diag1cat == . ;

/* Family structure */
tab toa, m;
gen byte n_fam_singmom=	(toa=="DM" | toa=="BM");
gen byte n_fam_nopar=	(toa=="DC" | toa=="BC");
tab n_fam_singmom, m;
tab n_fam_nopar, m;

/* Male */
tab n_male, m;

/* Severity */
tab dibmdr, m;
gen byte n_dibmdr_miss=(dibmdr=="");
gen byte n_dibmdr_mip=(dibmdr=="3");
gen byte n_dibmdr_mine=(dibmdr=="5" | dibmdr=="7");
gen byte n_dibmdr_mie=(!n_dibmdr_miss & !n_dibmdr_mip & !n_dibmdr_mine);
tab n_dibmdr_mie, m;
tab n_dibmdr_mip, m;
tab n_dibmdr_mine, m;

/* Age at entry */
tab n_firstage, m;
replace n_firstage = 999 if n_firstage == . ;
tab n_firstage, m ;

/* Parent earnings and household income */
local earncut = 5000 ;

order n_hhearns1996_*  n_combpay_* n_hhmbayr1996_* n_hhcombpay1996_* n_sibpayall_* ;
forval yr=1990(1)2012 { ;
	gen n_inchh1996_`yr' = n_hhearns1996_`yr' + n_combpay_`yr' + n_hhmbayr1996_`yr' + n_hhcombpay1996_`yr' + n_sibpayall_`yr' if n_hhearns1996_`yr' != . ;
} ;
forval yr=1990(1)2012 { ;
	gen n_inchh_`yr' = n_hhearns_`yr' + n_combpay_`yr' + n_hhmbayr_`yr' + n_hhcombpay_`yr' + n_sibpayall_`yr' if n_hhearns_`yr' != . ;
} ;
summ n_inchh* ;

egen n_earnhh9702=rowmean(n_hhearns_1997-n_hhearns_2002) ;
egen n_earnhh9712=rowmean(n_hhearns_1997-n_hhearns_2012) ;
egen n_earnhhpre9095=rowmean(n_hhearns_1990-n_hhearns_1995) ;

egen n_inchh9702=rowmean(n_inchh_1997-n_inchh_2002) ;
egen n_inchh9712=rowmean(n_inchh_1997-n_inchh_2012) ;
egen n_inchhpre9095=rowmean(n_inchh_1990-n_inchh_1995) ;

forval yr=1990(1)2012 { ;
	gen byte temp`yr' = (n_hhearns_`yr' > `earncut') ;
	replace temp`yr' = . if n_hhearns_`yr' == . ;
} ;
egen n_emphhpost9702=rowmean(temp1997-temp2002) ;
egen n_emphhpost9712=rowmean(temp1997-temp2012) ;
egen n_emphhpre9095=rowmean(temp1990-temp1995) ;
drop temp* ;

xtile n_earnhhbins = n_earnhhpre9095, nq(10) ;
tab n_earnhhbins, m ;
replace n_earnhhbins = 99 if n_earnhhbins == . ;
tab n_earnhhbins, m ;

/* First stage variables */
gen byte n_age18unfav=(age18_final_con==0);
egen n_onssi1922 = rowmean(n_onssi_1997-n_onssi_2000) ;

gen yob = year(dob) ;
summ yob ;
local yob_min = r(min) ;
local yob_max = r(max) ;
gen mob = month(dob) ;

forval yr = 1995(1)2003 { ;
	forval mo = 1(1)12 { ;
		gen byte n_onssimo_`yr'_`mo' = (combpmt_`yr'_`mo' > 0 & combpmt_`yr'_`mo' !=.) if combpmt_`yr'_`mo' !=. ;
	} ;
} ;

gen n_onssi1922_exact = . ;
forval yob = `yob_min'(1)`yob_max' { ;
	forval mob =1(1)12 { ;
		egen temp_`yob'_`mob' = rowmean(n_onssimo_`=`yob'+19'_`=`mob''-n_onssimo_`=`yob'+22'_`=`mob'') if yob == `yob' & mob == `mob' ;
		replace n_onssi1922_exact = temp_`yob'_`mob' if yob == `yob' & mob == `mob' ;
	} ;
} ;

gen n_onssi20 = . ;
forval yob = `yob_min'(1)`yob_max' { ;
	forval mob = 1(1)12 { ;
		replace n_onssi20 = n_onssimo_`=`yob'+20'_`mob' if yob == `yob' & mob == `mob' ;
	} ;
} ;

drop yob mob temp* ;
summ n_age18unfav n_onssi1922 n_onssi1922_exact n_onssi20 ;

/* Mortality */
gen numi_dob_mo = substr(numi_dob,1,2) ;
	replace numi_dob_mo = "" if numi_dob_mo == "00" | numi_dob_mo == "XX" ;
gen numi_dob_dd = substr(numi_dob,3,2) ;
	replace numi_dob_dd = "" if numi_dob_dd == "00" | numi_dob_dd == "XX" ;
gen numi_dob_yy = substr(numi_dob,5,4) ;
destring numi_dob_mo, replace ;
destring numi_dob_dd, replace ;
destring numi_dob_yy, replace ;
drop numi_dob ;
gen numi_dob = mdy(numi_dob_mo, numi_dob_dd, numi_dob_yy) ;

forval i = 1(1)3 { ;
	gen numi_dod`i'_mo = substr(numi_dod`i',1,2) ;
	gen numi_dod`i'_dd = substr(numi_dod`i',3,2) ;
		replace numi_dod`i'_dd = "" if numi_dod`i'_dd == "00" | numi_dod`i'_dd == "XX" ;
	gen numi_dod`i'_yy = substr(numi_dod`i',5,4) ;
	destring numi_dod`i'_mo, replace ;
	destring numi_dod`i'_dd, replace ;
	destring numi_dod`i'_yy, replace ;
	drop numi_dod`i' ;
	gen numi_dod`i' = mdy(numi_dod`i'_mo, numi_dod`i'_dd, numi_dod`i'_yy) ;
} ;
gen numi_dod = numi_dod1 ;
replace numi_dod = numi_dod2 if numi_dod == . & numi_dod2 != . ;
replace numi_dod = numi_dod3 if numi_dod == . & numi_dod3 != . ;
drop numi_dod1* numi_dod2* numi_dod3* ;
gen numi_yod = year(numi_dod) ;
gen byte numi_dthpost = (numi_yod > 1996 & numi_yod !=.) ;
tab numi_dthpost, m ;


/* ********************************* */
/* **** CREATE RUNNING VARIABLE **** */
/* ********************************* */
gen dobdd_stata	=mdy(month(dob),day(dob),year(dob)+18); 
gen dobdd_run	=dobdd_stata-mdy(08,22,1996);

gen dobwk_stata	=wofd(mdy(month(dob),day(dob),year(dob)+18));
gen dobwk_run	=dobwk_stata-wofd(mdy(08,22,1996));

gen dobmo_stata	=ym(year(dob)+18,month(dob)) ;
gen dobmo_run	=dobmo_stata-mofd(mdy(08,22,1996)) ;

/* ************************** */
/* **** SAMPLE SELECTION **** */
/* ************************** */

keep if year(first_pay_dte)<1996 ;
count;


/* ********************************************************************** */
/* **** CREATE CRIME VARIABLES USING DIFFERENT GEO/YEAR DENOMINATORS **** */
/* ********************************************************************** */

rename pdscc pdscc_full ;
gen pdscc = substr(pdscc_full,1,5) ;
gen temp_st = substr(pdscc_full,1,2) ;
tab temp_st, m ;
drop temp_st pdscc_full ;

merge m:1 pdscc using `cnty_year', keepusing(keep_* cnty_* flag*) ;
drop if _merge == 2 ;
drop _merge ;
foreach var of varlist keep* { ;
	replace `var' = 0 if `var' == . ;
} ;

summ keep* ;


/* ******************************** */
/* **** DEFINE CRIME VARIABLES **** */
/* ******************************** */

local char_cond 	2 ;
local drug_cond 	3 ;
local traf_cond     4 ;
local fel_cond		5 ;
local genr_cond		6 ;
local nogn_cond  	7 ;
local prop_cond 	8 ;
local pord_cond     9 ;
local viol_cond 	10 ;
local wknd_cond		11 ;
local fcvn_cond		12 ;
local acvn_cond     13 ;
local inca_cond 	14 ;
local par_cond 		15 ;
local prob_cond 	16 ;

local dgsl_cond 1 ;
local dgot_cond 2 ;
local dgps_cond 3 ;
local polq_cond	4 ;
local poot_cond	5 ;
local povc_cond	6 ;
local povl_cond	7 ;
local powp_cond	8 ;
local prbg_cond 9 ;
local prfd_cond 10 ;
local prtf_cond 11 ;
local vrob_cond 12 ;
local vlct_cond 13 ;
local vlpa_cond 14 ;
local vlpr_cond 15 ;
local hinc_cond 16 ;

local vcch_cond 1 ;
local vcgb_cond 2 ;
local vcpd_cond 3 ;
local vcpp_cond 4 ;
local vcps_cond 5 ;

local nias_cond 1 ; 
local nide_cond 2 ; 
local nidc_cond 3 ; 
local nidp_cond 4 ; 
local nidu_cond 5 ; 
local nilq_cond 6 ; 
local niob_cond 7 ; 
local nisx_cond 8 ; 
local nitf_cond 9 ; 
local nitp_cond 10 ; 
local niwp_cond 11 ;	

local any_abbr		"any" ;
local char_abbr 	"2c" ;
local drug_abbr 	"3cd" ;
local traf_abbr 	"4ct" ;
local fel_abbr  	"5cf" ;
local genr_abbr 	"6cg" ;
local nogn_abbr     "7cn" ;
local prop_abbr 	"8cp" ;
local pord_abbr     "9co" ;
local viol_abbr 	"10cv" ;
local wknd_abbr 	"11cw" ;
local fcvn_abbr 	"12fc" ;
local acvn_abbr     "13fm" ;
local inca_abbr		"14ie" ;	
local par_abbr		"15pa" ;
local prob_abbr		"16pr" ;
local ipp_abbr		"ipp" ;
local jail_abbr		"jail" ;

foreach stub in 
	dgsl dgot dgps polq poot povc povl powp prbg prfd prtf vrob vlct vlpa vlpr hinc  
	nias nide nidc nidp nidu nilq niob nisx nitf nitp niwp
	vcch vcgb vcpd vcpp vcps
	{ ;
		local `stub'_abbr "``stub'_cond'`stub'" ;
	} ;

local type_list_main "char genr nogn drug viol prop pord fel fcvn acvn inca" ; 
local type_list_bk "dgsl dgot dgps polq poot povc povl powp prbg prfd prtf vrob vlct vlpa vlpr hinc" ;
local type_list_nobk "nias nide nidc nidp nidu nilq niob nisx nitf nitp niwp" ;
local type_list_vice "vcch vcgb vcpd vcpp vcps" ;
local type_list "`type_list_main' `type_list_bk' `type_list_nobk' `type_list_vice' " ;

foreach stub in `type_list_main' { ;
	local `stub'_event "event" ;
} ;
foreach stub in `type_list_bk' { ;
	local `stub'_event "bk_event" ;
} ;
foreach stub in `type_list_nobk' { ;
	local `stub'_event "nobk_event" ;
} ;
foreach stub in `type_list_vice' { ;
	local `stub'_event "vice_event" ;
} ;


rename indiv_match temp1 ;

forval i=1(1)9 { ;
	rename event_date0`i' event_date`i' ;
	rename event0`i' event`i' ;
	rename day_of_week0`i' day_of_week`i' ;
	rename event_state0`i' event_state`i' ;
} ;

foreach class in bk nobk vice { ;
	forval i=1(1)9 { ;
		rename `class'_event_date0`i' 	`class'_event_date`i' ;
		rename `class'_event0`i' 		`class'_event`i' ;
		rename `class'_event_state0`i' 	`class'_event_state`i' ;
	} ;
} ;

gen byte temp2 = (event_date1 != .) ;

forval i = 1(1)2 { ;
		gen n_crime`i'any_post = temp`i' if 	keep_`firstyr'post_any == 1 ;
} ;
drop temp* ;

foreach class in event bk_event nobk_event vice_event { ;
	forval i=1(1)20 { ;
		recast double `class'`i' ;
		tostring(`class'`i'), replace format(%18.0g) ;
		forval j = 3(1)9 { ;
			replace `class'`i' = subinstr(`class'`i',"`j'","2",.) ;
		} ;
	} ;
} ;

forval yr = 1997(1)2019 { ;
	gen days_in_ipp`yr' = days_in_prison`yr' + days_in_parole`yr' + days_in_probation`yr' ;
} ;

/* **** GENERIC **** */
forval start = `range' { ;
	foreach end in `list_`start'' { ;
	
		/* Indicator */
		gen 		n_crime2any_`start'_`end'= 0 
				if (keep_`start'_`end'_any == 1) ;
		forval i=1(1)20 { ;
			replace n_crime2any_`start'_`end' = 1 
				if (keep_`start'_`end'_any == 1)
				& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `end' ;
			} ;
			
		forval yr=`start'(1)`end' { ;
			replace n_crime2any_`start'_`end' = 1 
				if n_crime2any_`start'_`end' != . 
				& days_in_ipp`yr' > 0 & days_in_ipp`yr' != . ;
		} ;
		
		/* Total number */
		gen 		n_crime2anyt_`start'_`end'= 0 
				if (keep_`start'_`end'_any == 1) ;
		forval i=1(1)20 { ;
			replace n_crime2anyt_`start'_`end' = n_crime2anyt_`start'_`end' + 1 
				if (keep_`start'_`end'_any == 1)
				& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `end' ;
			} ;
			
		/* Cumulative */
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2any`yr'_`start'_`end'= 0 
					if (keep_`start'_`end'_any == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2any`yr'_`start'_`end' = 1
					if (keep_`start'_`end'_any == 1)
					& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `yr' ;
				} ;
			} ;
			
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2anyt`yr'_`start'_`end'= 0 
					if (keep_`start'_`end'_any == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2anyt`yr'_`start'_`end' = c_crime2anyt`yr'_`start'_`end' + 1 
					if (keep_`start'_`end'_any == 1)
					& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `yr' ;
				} ;
			} ;
				
	} ;	/* end end */
} ;	/* end start */


/* **** SPECIFIC TYPES OF CRIME **** */
foreach type in `type_list' { ;
	forval start = `range' { ;
		foreach end in `list_`start'' { ;
			
			/* Indicator */
			gen 		n_crime2`type'_`start'_`end'= 0 
					if (keep_`start'_`end'_``type'_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace n_crime2`type'_`start'_`end' = 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& year(``type'_event'_date`i' ) >= `start' & year(``type'_event'_date`i' ) <= `end' & substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;
				
			/* Total number */
			gen 		n_crime2`type't_`start'_`end'= 0 
					if (keep_`start'_`end'_``type'_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace n_crime2`type't_`start'_`end' = n_crime2`type't_`start'_`end' + 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& year(``type'_event'_date`i' ) >= `start' & year(``type'_event'_date`i' ) <= `end' & substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;
				
			} ;	/* end end */
		} ;	/* end start */
} ; /* type */

/* Special combinations */
forval start = `range' { ;
	foreach end in `list_`start'' { ;
		
		/* Indicator */
		gen 		n_crime2vnrb_`start'_`end'= 0 
				if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1) ;
		forval i=1(1)20 { ;
			replace n_crime2vnrb_`start'_`end' = 1 
				if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1)
				& year(`viol_event'_date`i' ) >= `start' & year(`viol_event'_date`i' ) <= `end' 
				& substr(`viol_event'`i',`viol_cond',1) == "2" & substr(`vrob_event'`i',`vrob_cond',1) != "2" ;
			} ;
			
		/* Total number */
		gen 		n_crime2vnrbt_`start'_`end'= 0 
				if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1) ;
		forval i=1(1)20 { ;
			replace n_crime2vnrbt_`start'_`end' = n_crime2vnrbt_`start'_`end' + 1 
				if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1)
				& year(`viol_event'_date`i' ) >= `start' & year(`viol_event'_date`i' ) <= `end' 
				& substr(`viol_event'`i',`viol_cond',1) == "2" & substr(`vrob_event'`i',`vrob_cond',1) != "2" ;
			} ;
			
	} ;	/* end end */
} ;	/* end start */

	
forval start = `range' { ;
	foreach end in `list_`start'' { ;
		
	egen n_crime2dgpo_`start'_`end'= rowmax(n_crime2dgps_`start'_`end' n_crime2dgot_`start'_`end')	 
		if n_crime2dgps_`start'_`end' != . & n_crime2dgot_`start'_`end' != . ;
	
	egen n_crime2dgpot_`start'_`end'= rowtotal(n_crime2dgpst_`start'_`end' n_crime2dgott_`start'_`end') 
		if n_crime2dgpst_`start'_`end' != . & n_crime2dgott_`start'_`end' != ., missing ;
		
	egen n_crime2nibd_`start'_`end'= rowmax(n_crime2nidc_`start'_`end' n_crime2niob_`start'_`end')	 
		if n_crime2nidc_`start'_`end' != . & n_crime2niob_`start'_`end' != . ;
	
	egen n_crime2nibdt_`start'_`end'= rowtotal(n_crime2nidct_`start'_`end' n_crime2niobt_`start'_`end') 
		if n_crime2nidct_`start'_`end' != . & n_crime2niobt_`start'_`end' != ., missing ;
		
	gen n_crime2sxdg_`start'_`end' = (n_crime2nisx_`start'_`end' == 1 &  n_crime2drug_`start'_`end' == 1)
		if n_crime2nisx_`start'_`end' != . &  n_crime2drug_`start'_`end' != . ;
		
	gen n_crime2sxvc_`start'_`end' = (n_crime2nisx_`start'_`end' == 1 &  n_crime2povc_`start'_`end' == 1)
		if n_crime2nisx_`start'_`end' != . &  n_crime2povc_`start'_`end' != . ;
		
	gen n_crime2gnvl_`start'_`end' = (n_crime2genr_`start'_`end' == 1 &  n_crime2vnrb_`start'_`end' == 1)
		if n_crime2genr_`start'_`end' != . &  n_crime2vnrb_`start'_`end' != . ;
		
	gen n_crime2vcot_`start'_`end' = (n_crime2povc_`start'_`end' == 1 
										&  n_crime2vcch_`start'_`end' != 1
										&  n_crime2vcgb_`start'_`end' != 1
										&  n_crime2vcpd_`start'_`end' != 1
										&  n_crime2vcpp_`start'_`end' != 1
										&  n_crime2vcps_`start'_`end' != 1
										)
		if n_crime2povc_`start'_`end' != . ;
		
	/* anti-social index (disorderly conduct, non-rob violent) */
	egen n_crime2anti_`start'_`end'= rowmax(n_crime2nidc_`start'_`end' n_crime2vnrb_`start'_`end')	 
		if n_crime2nidc_`start'_`end' != . & n_crime2vnrb_`start'_`end' != . ;
	
	egen n_crime2antit_`start'_`end'= rowtotal(n_crime2nidct_`start'_`end' n_crime2vnrbt_`start'_`end') 
		if n_crime2nidct_`start'_`end' != . & n_crime2vnrbt_`start'_`end' != ., missing ;
		
	/* income-requiring charge estimates (alcohol, drug, with and without weapons) */
	egen n_crime2req1_`start'_`end'= rowmax(n_crime2nidp_`start'_`end' n_crime2nidu_`start'_`end' n_crime2nilq_`start'_`end' n_crime2niwp_`start'_`end')	 
		if n_crime2nidp_`start'_`end' != . & n_crime2nidu_`start'_`end' != . & n_crime2nilq_`start'_`end' != . & n_crime2niwp_`start'_`end' != . ;
	
	egen n_crime2req1t_`start'_`end'= rowtotal(n_crime2nidpt_`start'_`end' n_crime2nidut_`start'_`end' n_crime2nilqt_`start'_`end' n_crime2niwpt_`start'_`end') 
		if n_crime2nidpt_`start'_`end' != . & n_crime2nidut_`start'_`end' != . & n_crime2nilqt_`start'_`end' != . & n_crime2niwpt_`start'_`end' != ., missing ;
		
	egen n_crime2req2_`start'_`end'= rowmax(n_crime2nidp_`start'_`end' n_crime2nidu_`start'_`end' n_crime2nilq_`start'_`end')	 
		if n_crime2nidp_`start'_`end' != . & n_crime2nidu_`start'_`end' != . & n_crime2nilq_`start'_`end' != . ;
	
	egen n_crime2req2t_`start'_`end'= rowtotal(n_crime2nidpt_`start'_`end' n_crime2nidut_`start'_`end' n_crime2nilqt_`start'_`end') 
		if n_crime2nidpt_`start'_`end' != . & n_crime2nidut_`start'_`end' != . & n_crime2nilqt_`start'_`end' != ., missing ;
		
	} ;	/* end end */
} ;	/* end start */


/* Year-by-year genr */
foreach type in genr { ;
	forval yr = 1997(1)2017 { ;
			
			/* Indicator */
			gen 		yr_crime2`type'_`yr' = 0 
					if (keep_1997_2017_``type'_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace yr_crime2`type'_`yr' = 1 
					if (keep_1997_2017_``type'_abbr' == 1)
					& year(``type'_event'_date`i' ) == `yr' & substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;
				
		} ;	/* end yr */
} ; /* type */


/* Timing of income-generating and violent */
forval start = `range' { ;
	foreach end in `list_`start'' { ;
	
		foreach type in viol genr { ;
			gen temp_first_`type'_`start'_`end' = . ;
			forval i=1(1)20 { ;
					replace temp_first_`type'_`start'_`end' = ``type'_event'_date`i' 
					if substr(``type'_event'`i',``type'_cond',1) == "2" 
					& year(``type'_event'_date`i' ) >= `start' & year(``type'_event'_date`i' ) <= `end' ;
			} ;
		} ;
		
	gen 		n_crime2vtheng_`start'_`end'= 0 
			if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`genr_abbr' == 1) ;
	replace 	n_crime2vtheng_`start'_`end'= 1 
			if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`genr_abbr' == 1)
				& temp_first_viol != . & temp_first_genr != . & temp_first_viol + 30 < temp_first_genr 
			;
			
	gen 		n_crime2gthenv_`start'_`end'= 0 
			if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`genr_abbr' == 1) ;
	replace 	n_crime2gthenv_`start'_`end'= 1 
			if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`genr_abbr' == 1)
				& temp_first_viol != . & temp_first_genr != . & temp_first_genr + 30 < temp_first_viol 
			;
			
	gen 		n_crime2gvsame_`start'_`end'= 0 
			if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`genr_abbr' == 1) ;
	replace 	n_crime2gvsame_`start'_`end'= 1 
			if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`genr_abbr' == 1)
				& temp_first_viol != . & temp_first_genr != . & abs(temp_first_genr - temp_first_viol) <= 30  
			;
	
	} ;	/* end end */
} ;	/* end start */	
drop temp_* ;
	
	
/* Cumulative */
foreach type in `type_list' { ;
	forval start = `range' { ;
		foreach end in `list_`start'' { ;
			
			forval yr = `=`end'-(4*4)'(4)`end' { ;
				gen 		c_crime2`type'`yr'_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace c_crime2`type'`yr'_`start'_`end' = 1 
						if (keep_`start'_`end'_``type'_abbr' == 1)
						& year(``type'_event'_date`i' ) >= `start' & year(``type'_event'_date`i' ) <= `yr' & substr(``type'_event'`i',``type'_cond',1) == "2" ;
					} ;
				} ;
				
			forval yr = `=`end'-(4*4)'(4)`end' { ;
				gen 		c_crime2`type't`yr'_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace c_crime2`type't`yr'_`start'_`end' = c_crime2`type't`yr'_`start'_`end' + 1 
						if (keep_`start'_`end'_``type'_abbr' == 1)
						& year(``type'_event'_date`i' ) >= `start' & year(``type'_event'_date`i' ) <= `yr' & substr(``type'_event'`i',``type'_cond',1) == "2" ;
					} ;
				} ;
				
			} ;	/* end end */
		} ;	/* end start */
} ; /* type */

forval start = `range' { ;
	foreach end in `list_`start'' { ;
		forval yr = `=`end'-(4*4)'(4)`end' { ;
		
		egen c_crime2dgpo`yr'_`start'_`end' = rowmax(c_crime2dgps`yr'_`start'_`end' c_crime2dgot`yr'_`start'_`end')	 
			if c_crime2dgps`yr'_`start'_`end' != . & c_crime2dgot`yr'_`start'_`end' != . ;
		
		egen c_crime2dgpot`yr'_`start'_`end' = rowtotal(c_crime2dgpst`yr'_`start'_`end' c_crime2dgott`yr'_`start'_`end') 
			if c_crime2dgpst`yr'_`start'_`end' != . & c_crime2dgott`yr'_`start'_`end' != ., missing ;
			
		egen c_crime2nibd`yr'_`start'_`end' = rowmax(c_crime2nidc`yr'_`start'_`end' c_crime2niob`yr'_`start'_`end')	 
			if c_crime2nidc`yr'_`start'_`end' != . & c_crime2niob`yr'_`start'_`end' != . ;
		
		egen c_crime2nibdt`yr'_`start'_`end' = rowtotal(c_crime2nidct`yr'_`start'_`end' c_crime2niobt`yr'_`start'_`end') 
			if c_crime2nidct`yr'_`start'_`end' != . & c_crime2niobt`yr'_`start'_`end' != ., missing ;
			
		} ; /* end yr */
	} ;	/* end end */
} ;	/* end start */

forval start = `range' { ;
	foreach end in `list_`start'' { ;
		
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2vnrb`yr'_`start'_`end'= 0 
					if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2vnrb`yr'_`start'_`end' = 1 
					if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1)
					& year(`viol_event'_date`i' ) >= `start' & year(`viol_event'_date`i' ) <= `yr' 
					& substr(`viol_event'`i',`viol_cond',1) == "2" & substr(`vrob_event'`i',`vrob_cond',1) != "2" ;
				} ;
			} ;
			
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2vnrbt`yr'_`start'_`end'= 0 
					if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2vnrbt`yr'_`start'_`end' = c_crime2vnrbt`yr'_`start'_`end' + 1 
					if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1)
					& year(`viol_event'_date`i' ) >= `start' & year(`viol_event'_date`i' ) <= `yr' 
					& substr(`viol_event'`i',`viol_cond',1) == "2" & substr(`vrob_event'`i',`vrob_cond',1) != "2" ;
				} ;
			} ;
			
		} ;	/* end end */
	} ;	/* end start */
		
		
		


/* Incarceration, parole, or probation FLOW */
forval start = `range' { ;
	foreach end in `list_`start'' { ;
		
		/* Indicator */
		gen 		n_crime2ipp_`start'_`end'= 0 
				if (keep_`start'_`end'_ipp == 1 ) ;
		forval i=1(1)20 { ;
			replace n_crime2ipp_`start'_`end' = 1 
				if (keep_`start'_`end'_ipp == 1 )
				& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `end' 
				& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
			} ;
			
		/* Total number */
		gen 		n_crime2ippt_`start'_`end'= 0 
				if (keep_`start'_`end'_ipp == 1 ) ;
		forval i=1(1)20 { ;
			replace n_crime2ippt_`start'_`end' = n_crime2ippt_`start'_`end' + 1 
				if (keep_`start'_`end'_ipp == 1 )
				& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `end' 
				& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
			} ;
			
		/* Cumulative */
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2ipp`yr'_`start'_`end'= 0 
					if (keep_`start'_`end'_ipp == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2ipp`yr'_`start'_`end' = 1 
					if (keep_`start'_`end'_ipp == 1)
					& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `yr' 
					& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
				} ;
			} ;
			
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2ippt`yr'_`start'_`end'= 0 
					if (keep_`start'_`end'_ipp == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2ippt`yr'_`start'_`end' = c_crime2ippt`yr'_`start'_`end' + 1 
					if (keep_`start'_`end'_ipp == 1)
					& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `yr' 
					& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
				} ;
			} ;
		
		} ;	/* end end */
} ;	/* end start */


/* Length of IPP spells / indicator for any IPP */
local full_hinc "hprison" ;
local full_inca "prison" ;
local full_par  "parole" ;
local full_prob "probation" ;
local full_ipp 	"ipp" ;
local full_jail "jail" ;

local correct_list "hinc inca par prob ipp jail" ; 

foreach type in `correct_list' { ;
	
	forval start = `range' { ;
			foreach end in `list_`start'' { ;
				egen 		n_crime2s`type'_`start'_`end' = rowmean(days_in_`full_`type''`start'-days_in_`full_`type''`end') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;	
				
				/* cumulative */
				forval yr = `start'(4)`end' { ;
					egen c_crime2s`type'`yr'_`start'_`end' = rowtotal(days_in_`full_`type''1997-days_in_`full_`type''`yr') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;
				} ;
				
			} ;	/* end end */
	} ;	/* end start */

} ; /* end type */

foreach type in `correct_list' { ;
	forval i=`firstyr'(1)`lastyr' { ;
		gen n_yrcrime2s`type'_`i' = days_in_`full_`type''`i' if (keep_yr`i'_`type' == 1) ;
	} ;
} ;

foreach abbr in `correct_list' { ;
	forval yr = 1997(1)2019 { ;
		gen byte any_`abbr'`yr' = (days_in_`full_`abbr''`yr' > 0) ;
		replace  any_`abbr'`yr' = . if days_in_`full_`abbr''`yr' == . ;
	} ;
} ;

foreach type in `correct_list' { ;
	
	forval start = `range' { ;
			foreach end in `list_`start'' { ;
			
				egen 		n_crime2a`type'_`start'_`end' = rowmean(any_`type'`start'-any_`type'`end') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;
				
				egen 		n_crime2c`type'_`start'_`end' = rowmax(any_`type'`start'-any_`type'`end') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;
				
				forval yr = `=`end'-(4*4)'(4)`end' { ;
					egen c_crime2c`type'`yr'_`start'_`end' = rowmax(any_`type'1997-any_`type'`yr') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;
				} ;
				
			} ;	/* end end */
	} ;	/* end start */

} ; /* end type */

foreach type in `correct_list' { ;
	forval i=`firstyr'(1)`lastyr' { ;
		gen n_yrcrime2a`type'_`i' = any_`type'`i' if (keep_yr`i'_`type' == 1) ;
	} ;
} ;


/* **** WEEKDAY/WEEKEND **** */
forval i=1(1)20 { ;
	replace day_of_week`i' = 7 if day_of_week`i' == 0 ;
} ; 

local week_min 1 ;
local week_max 4 ;
local wknd_min 5 ;
local wknd_max 7 ;

local genr_first "g" ;
local char_first "c" ;
local inca_first "i" ;

foreach wk in week wknd { ;
	forval start = `range' { ;;
		foreach end in `list_`start'' { ;
		
			/* Any type */
			gen 		n_crime2`wk'_`start'_`end'= 0 
					if (keep_`start'_`end'_any == 1) ;
			forval i=1(1)20 { ;
				replace n_crime2`wk'_`start'_`end' = 1 
					if (keep_`start'_`end'_any == 1)
					& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `end' & (day_of_week`i' >= ``wk'_min' & day_of_week`i' <= ``wk'_min') ;
				} ;
				
			gen 		n_crime2`wk't_`start'_`end'= 0 
					if (keep_`start'_`end'_any == 1) ;
			forval i=1(1)20 { ;
				replace n_crime2`wk't_`start'_`end' = n_crime2`wk't_`start'_`end' + 1 
					if (keep_`start'_`end'_any == 1)
					& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `end' & (day_of_week`i' >= ``wk'_min' & day_of_week`i' <= ``wk'_min') ;
				} ;
				
			/* Specific crimes */
			foreach type in genr { ;
				gen 		n_crime2`wk'``type'_first'_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace n_crime2`wk'``type'_first'_`start'_`end' = 1 
						if (keep_`start'_`end'_``type'_abbr' == 1)
						& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `end' & (day_of_week`i' >= ``wk'_min' & day_of_week`i' <= ``wk'_min') 
						& substr(event`i',``type'_cond',1) == "2" ;
					} ;
					
				gen 		n_crime2`wk'``type'_first't_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace n_crime2`wk'``type'_first't_`start'_`end' = n_crime2`wk'``type'_first't_`start'_`end' + 1 
						if (keep_`start'_`end'_``type'_abbr' == 1)
						& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `end' & (day_of_week`i' >= ``wk'_min' & day_of_week`i' <= ``wk'_min') 
						& substr(event`i',``type'_cond',1) == "2" ;
					} ;
			} ;
				
		} ;	/* end end */
	} ;	/* end start */

} ; /*end wk */



/* **** CRIME AND EMPLOYMENT JOINT OUTCOME **** */
local empcut = 0.5 ;
forval yr=`firstyr'(1)`lastearnyr' { ;
	rename earn`yr' n_yrkidearn`yr' ;
	forval amt = 5(10)15 { ;
		gen byte n_yrkidemp`amt'_`yr' = (n_yrkidearn`yr' > `amt' * 1000) ;
		replace  n_yrkidemp`amt'_`yr' = . if n_yrkidearn`yr' == . ;
	} ;
} ;

order n_yrkidearn* ;
forval amt = 5(10)15 { ;
	order n_yrkidemp`amt'* ;
} ;

/* Kid earnings and employment */
	forval start = `range' { ;
		foreach end in `list_`start'' { ;
			egen avg_kidearn_`start'_`end' = rowmean(n_yrkidearn`start'-n_yrkidearn`end') if keep_`start'_`end'_any == 1 ;
		} ;
	} ;

	forval start = `range' { ;
		foreach end in `list_`start'' { ;
			forval amt = 5(10)15 { ;
				egen avg_kidemp`amt'_`start'_`end' = rowmean(n_yrkidemp`amt'_`start'-n_yrkidemp`amt'_`end') if keep_`start'_`end'_any == 1 ;
			} ;
		} ;
	} ;

/* On SSI */
foreach var in onssi combpay { ;
	forval yr=2015(1)2019 { ;
		gen double n_`var'_`yr' = n_`var'_2014 ;		
	} ;
	order n_`var'_* ;

	egen avg_`var'_post = rowmean(n_`var'_`firstyr'-n_`var'_2019) if keep_`firstyr'post_any == 1 ;

	forval start = `range' { ;
	foreach end in `list_`start'' { ;
		egen avg_`var'_`start'_`end' = rowmean(n_`var'_`start'-n_`var'_`end') if keep_`start'_`end'_any == 1 ;
	} ;
	} ;
} ;

forval yr=`firstyr'(1)`lastearnyr' { ;
		gen n_yrkidinc`yr' = n_yrkidearn`yr' + n_combpay_`yr' ;
	} ;
	forval yr=`firstyr'(1)2012 { ;
		gen n_yrkidinchh`yr' = n_yrkidearn`yr' + n_combpay_`yr' + n_hhearns_`yr' ;
} ;


local list_emp "char genr inca" ;

forval start = `range' { ;
	foreach end in `list_`start'' { ;
		forval amt = 5(10)15 { ;
	
			/* Crime and employed (generic and specific) */
			foreach type in `list_emp' { ;
				gen 		n_crime2emp`amt'``type'_first'_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				replace n_crime2emp`amt'``type'_first'_`start'_`end' = 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& n_crime2`type'_`start'_`end' == 1
					& avg_kidemp`amt'_`start'_`end' >= `empcut' & avg_kidemp`amt'_`start'_`end' != . ;
			} ;
			
			foreach type in `list_emp' { ;
				gen 		n_crime2emp`amt'``type'_first't_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				replace n_crime2emp`amt'``type'_first't_`start'_`end' = 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& n_crime2`type't_`start'_`end' > 1 & n_crime2`type't_`start'_`end' != .
					& avg_kidemp`amt'_`start'_`end' >= `empcut' & avg_kidemp`amt'_`start'_`end' != . ;
			} ;
			
			/* Crime and not employed (generic and specific) */
			foreach type in `list_emp' { ;
				gen 		n_crime2noemp`amt'``type'_first'_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				replace n_crime2noemp`amt'``type'_first'_`start'_`end' = 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& n_crime2`type'_`start'_`end' == 1
					& avg_kidemp`amt'_`start'_`end' < `empcut' & avg_kidemp`amt'_`start'_`end' != . ;
			} ;
			
			foreach type in `list_emp' { ;
				gen 		n_crime2noemp`amt'``type'_first't_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				replace n_crime2noemp`amt'``type'_first't_`start'_`end' = 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& n_crime2`type't_`start'_`end' > 1 & n_crime2`type't_`start'_`end' != .
					& avg_kidemp`amt'_`start'_`end' < `empcut' & avg_kidemp`amt'_`start'_`end' != . ;
			} ;
			
			/* No crime and employed */
			foreach type in `list_emp' { ;
				gen 		n_crimeno2emp`amt'``type'_first'_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				replace n_crimeno2emp`amt'``type'_first'_`start'_`end' = 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& n_crime2`type'_`start'_`end' == 0
					& avg_kidemp`amt'_`start'_`end' >= `empcut' & avg_kidemp`amt'_`start'_`end' != . ;
			} ;
			
			foreach type in `list_emp' { ;
				gen 		n_crimeno2emp`amt'``type'_first't_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				replace n_crimeno2emp`amt'``type'_first't_`start'_`end' = 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& n_crime2`type't_`start'_`end' <= 1
					& avg_kidemp`amt'_`start'_`end' >= `empcut' & avg_kidemp`amt'_`start'_`end' != . ;
			} ;
			
			/* No crime and not employed */
			foreach type in `list_emp' { ;
				gen 		n_crimeno2noe`amt'``type'_first'_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				replace n_crimeno2noe`amt'``type'_first'_`start'_`end' = 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& n_crime2`type'_`start'_`end' == 0
					& avg_kidemp`amt'_`start'_`end' < `empcut' & avg_kidemp`amt'_`start'_`end' != . ;
			} ;
			
			foreach type in `list_emp' { ;
				gen 		n_crimeno2noe`amt'``type'_first't_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				replace n_crimeno2noe`amt'``type'_first't_`start'_`end' = 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& n_crime2`type't_`start'_`end' <= 1
					& avg_kidemp`amt'_`start'_`end' < `empcut' & avg_kidemp`amt'_`start'_`end' != . ;
			} ;
			
		} ; /* end amt */
	} ;	/* end per */
} ;	/* end start */



/* *********** USE 2000-2016 DENOMINATOR AND VARY NUMERATOR: 2000-2004, 2005-2009, 2010-2016 ******* */
local num_per = 4 ;
local begin_1 = 1997 ;
local end_1   = 2001 ;
local begin_2 = 2002 ;
local end_2   = 2006 ;
local begin_3 = 2007 ;
local end_3   = 2012 ;
local begin_4 = 2013 ;
local end_4   = 2017 ;

forval j=1(1)`num_per' { ;

/* Any */
		gen 		n_`j'crime2any_1997_2017 = 0 	
			if (keep_1997_2017_any == 1) ;
		forval i=1(1)20 { ;
			replace n_`j'crime2any_1997_2017 = 1 	
			if (keep_1997_2017_any == 1) & year(event_date`i' ) >= `begin_`j'' & year(event_date`i' ) <= `end_`j'' ;
			} ;
		
		gen 		n_`j'crime2anyt_1997_2017= 0 
				if (keep_1997_2017_any == 1) ;
		forval i=1(1)20 { ;
			replace n_`j'crime2anyt_1997_2017 = n_`j'crime2anyt_1997_2017 + 1 
				if (keep_1997_2017_any == 1) & year(event_date`i' ) >= `begin_`j'' & year(event_date`i' ) <= `end_`j'' ;
			} ;
			
/* Specific type */		
foreach type in `type_list' { ;

		gen 		n_`j'crime2`type'_1997_2017 = 0 	
			if (keep_1997_2017_``type'_abbr' == 1) ;
		forval i=1(1)20 { ;
			replace n_`j'crime2`type'_1997_2017 = 1 	
			if (keep_1997_2017_``type'_abbr' == 1) 
			& year(``type'_event'_date`i' ) >= `begin_`j'' & year(``type'_event'_date`i' ) <= `end_`j'' 
			& substr(``type'_event'`i',``type'_cond',1) == "2" ;
			} ;
		
		gen 		n_`j'crime2`type't_1997_2017 = 0 
				if (keep_1997_2017_``type'_abbr' == 1) ;
		forval i=1(1)20 { ;
			replace n_`j'crime2`type't_1997_2017 = n_`j'crime2`type't_1997_2017 + 1 
				if (keep_1997_2017_``type'_abbr' == 1) 
				& year(``type'_event'_date`i' ) >= `begin_`j'' & year(``type'_event'_date`i' ) <= `end_`j'' 
				& substr(``type'_event'`i',``type'_cond',1) == "2" ;
			} ;		
			
} ;	/* end type */
		
	
	gen 		n_`j'crime2vnrb_1997_2017 = 0 	
			if (keep_1997_2017_`viol_abbr' == 1 & keep_1997_2017_`vrob_abbr' == 1) ;
		forval i=1(1)20 { ;
			replace n_`j'crime2vnrb_1997_2017 = 1 	
			if (keep_1997_2017_`viol_abbr' == 1 & keep_1997_2017_`vrob_abbr' == 1)
			& year(`viol_event'_date`i' ) >= `begin_`j'' & year(`viol_event'_date`i' ) <= `end_`j'' 
			& substr(`viol_event'`i',`viol_cond',1) == "2" & substr(`vrob_event'`i',`vrob_cond',1) != "2"  ;
			} ;
		
		gen 		n_`j'crime2vnrbt_1997_2017 = 0 
				if (keep_1997_2017_`viol_abbr' == 1 & keep_1997_2017_`vrob_abbr' == 1) ;
		forval i=1(1)20 { ;
			replace n_`j'crime2vnrbt_1997_2017 = n_`j'crime2vnrbt_1997_2017 + 1 
				if (keep_1997_2017_`viol_abbr' == 1 & keep_1997_2017_`vrob_abbr' == 1) 
				& year(`viol_event'_date`i' ) >= `begin_`j'' & year(`viol_event'_date`i' ) <= `end_`j'' 
				& substr(`viol_event'`i',`viol_cond',1) == "2" & substr(`vrob_event'`i',`vrob_cond',1) != "2" ;
				} ;
				
		egen n_`j'crime2dgpo_1997_2017 = rowmax(n_`j'crime2dgps_1997_2017 n_`j'crime2dgot_1997_2017)	 
		if n_`j'crime2dgps_1997_2017 != . & n_`j'crime2dgot_1997_2017 != . ;
	
		egen n_`j'crime2dgpot_1997_2017 = rowtotal(n_`j'crime2dgpst_1997_2017 n_`j'crime2dgott_1997_2017) 
			if n_`j'crime2dgpst_1997_2017 != . & n_`j'crime2dgott_1997_2017 != ., missing ;
			
		egen n_`j'crime2nibd_1997_2017 = rowmax(n_`j'crime2nidc_1997_2017 n_`j'crime2niob_1997_2017)	 
			if n_`j'crime2nidc_1997_2017 != . & n_`j'crime2niob_1997_2017 != . ;
		
		egen n_`j'crime2nibdt_1997_2017 = rowtotal(n_`j'crime2nidct_1997_2017 n_`j'crime2niobt_1997_2017) 
			if n_`j'crime2nidct_1997_2017 != . & n_`j'crime2niobt_1997_2017 != ., missing ;
			
		gen n_`j'crime2sxdg_1997_2017 = (n_`j'crime2nisx_1997_2017 == 1 & n_`j'crime2drug_1997_2017 == 1) 
			if n_`j'crime2nisx_1997_2017 != . &  n_`j'crime2drug_1997_2017 != . ;
			
		gen n_`j'crime2sxvc_1997_2017 = (n_`j'crime2nisx_1997_2017 == 1 & n_`j'crime2povc_1997_2017 == 1) 
			if n_`j'crime2nisx_1997_2017 != . &  n_`j'crime2povc_1997_2017 != . ;
			
		gen n_`j'crime2gnvl_1997_2017 = (n_`j'crime2genr_1997_2017 == 1 &  n_`j'crime2vnrb_1997_2017 == 1)
			if n_`j'crime2genr_1997_2017 != . &  n_`j'crime2vnrb_1997_2017 != . ;
			
		gen n_`j'crime2vcot_1997_2017 = (n_`j'crime2povc_1997_2017 == 1 
										&  n_`j'crime2vcch_1997_2017 != 1
										&  n_`j'crime2vcgb_1997_2017 != 1
										&  n_`j'crime2vcpd_1997_2017 != 1
										&  n_`j'crime2vcpp_1997_2017 != 1
										&  n_`j'crime2vcps_1997_2017 != 1
										)
		if n_`j'crime2povc_1997_2017 != . ;
		

/* IPP FLOW */
		gen 		n_`j'crime2ipp_1997_2017 = 0 	
			if (keep_1997_2017_ipp == 1 ) ;
		forval i=1(1)20 { ;
			replace n_`j'crime2ipp_1997_2017 = 1 	
			if (keep_1997_2017_ipp == 1 )
			& year(event_date`i' ) >= `begin_`j'' & year(event_date`i' ) <= `end_`j'' 
			& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
			} ;
		
		gen 		n_`j'crime2ippt_1997_2017= 0 
				if (keep_1997_2017_ipp == 1 ) ;
		forval i=1(1)20 { ;
			replace n_`j'crime2ippt_1997_2017 = n_`j'crime2ippt_1997_2017 + 1 
				if (keep_1997_2017_ipp == 1 )
				& year(event_date`i' ) >= `begin_`j'' & year(event_date`i' ) <= `end_`j'' 
				& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
			} ;
			
	/* IPP stock */
	foreach type in `correct_list' { ;
		egen 	n_`j'crime2s`type'_1997_2017 = rowmean(days_in_`full_`type''`begin_`j''-days_in_`full_`type''`end_`j'') if keep_1997_2017_``type'_abbr' == 1 ;
	} ;
	
	foreach type in `correct_list' { ;
		egen 	n_`j'crime2a`type'_1997_2017 = rowmean(any_`type'`begin_`j''-any_`type'`end_`j'') if keep_1997_2017_``type'_abbr' == 1 ;
		
		egen 	n_`j'crime2c`type'_1997_2017 = rowmax(any_`type'`begin_`j''-any_`type'`end_`j'') if keep_1997_2017_``type'_abbr' == 1 ;
	} ;
	
	/* Cumulative up to end of that period */
	foreach type in `correct_list' { ;
		egen 	n_`j'crime2cc`type'_1997_2017 = rowmax(any_`type'1997-any_`type'`end_`j'') if keep_1997_2017_``type'_abbr' == 1 ;
	} ;
	
	/* Weekday/weekend */		
	foreach wk in week wknd { ;

		gen 		n_`j'crime2`wk'_1997_2017 = 0 	
			if (keep_1997_2017_any == 1) ;
		forval i=1(1)20 { ;
			replace n_`j'crime2`wk'_1997_2017 = 1 	
			if (keep_1997_2017_any == 1) 
			& year(event_date`i' ) >= `begin_`j'' & year(event_date`i' ) <= `end_`j'' & (day_of_week`i' >= ``wk'_min' & day_of_week`i' <= ``wk'_min') ;
			} ;
		
		gen 		n_`j'crime2`wk't_1997_2017 = 0 
				if (keep_1997_2017_any == 1) ;
		forval i=1(1)20 { ;
			replace n_`j'crime2`wk't_1997_2017 = n_`j'crime2`wk't_1997_2017 + 1 
				if (keep_1997_2017_any == 1) 
				& year(event_date`i' ) >= `begin_`j'' & year(event_date`i' ) <= `end_`j'' & (day_of_week`i' >= ``wk'_min' & day_of_week`i' <= ``wk'_min') ;
			} ;		
	} ;	/* end wk */
	
	/* Crime and employment interaction */	
	foreach var in onssi combpay { ;
		egen avg_`j'`var'_1997_2017 = rowmean(n_`var'_`begin_`j''-n_`var'_`end_`j'')  if keep_1997_2017_any == 1 ;
	} ;
	egen avg_`j'kidearn_1997_2017 = rowmean(n_yrkidearn`begin_`j''-n_yrkidearn`end_`j'') 	if keep_1997_2017_any == 1 ;
	forval amt = 5(10)15 { ;
		egen avg_`j'kidemp`amt'_1997_2017  = rowmean(n_yrkidemp`amt'_`begin_`j''-n_yrkidemp`amt'_`end_`j'') 	if keep_1997_2017_any == 1 ;
	} ;
	
	/* Income by period */
	egen avg_`j'kidinc_1997_2017  = rowmean(n_yrkidinc`begin_`j'' -n_yrkidinc`end_`j'') 	if keep_1997_2017_any == 1 ;	
	if "`j'" != "4" { ;
		egen avg_`j'kidinchh_1997_2017  = rowmean(n_yrkidinchh`begin_`j'' -n_yrkidinchh`end_`j'') 	if keep_1997_2017_any == 1 ;
	} ;
	
	forval amt = 5(10)15 { ;
	
		/* Crime and employment */
		foreach type in `list_emp' { ;
			gen 		n_`j'crime2emp`amt'``type'_first'_1997_2017 = 0 
					if (keep_1997_2017_``type'_abbr' == 1) ;
			replace n_`j'crime2emp`amt'``type'_first'_1997_2017 = 1 
				if (keep_1997_2017_``type'_abbr' == 1)
				& n_`j'crime2`type'_1997_2017 == 1
				& avg_`j'kidemp`amt'_1997_2017 >= `empcut' & avg_`j'kidemp`amt'_1997_2017 != . ;
		} ;
		
		foreach type in `list_emp' { ;
			gen 		n_`j'crime2emp`amt'``type'_first't_1997_2017 = 0 
					if (keep_1997_2017_``type'_abbr' == 1) ;
			replace n_`j'crime2emp`amt'``type'_first't_1997_2017 = 1 
				if (keep_1997_2017_``type'_abbr' == 1)
				& n_`j'crime2`type't_1997_2017 > 1 & n_`j'crime2`type't_1997_2017 != .
				& avg_`j'kidemp`amt'_1997_2017 >= `empcut' & avg_`j'kidemp`amt'_1997_2017 != . ;
		} ;
		
		/* Crime and no employment */
		foreach type in `list_emp' { ;
			gen 		n_`j'crime2noemp`amt'``type'_first'_1997_2017 = 0 
					if (keep_1997_2017_``type'_abbr' == 1) ;
			replace n_`j'crime2noemp`amt'``type'_first'_1997_2017 = 1 
				if (keep_1997_2017_``type'_abbr' == 1)
				& n_`j'crime2`type'_1997_2017 == 1
				& avg_`j'kidemp`amt'_1997_2017 < `empcut' & avg_`j'kidemp`amt'_1997_2017 != . ;
		} ;
		
		foreach type in `list_emp' { ;
			gen 		n_`j'crime2noemp`amt'``type'_first't_1997_2017 = 0 
					if (keep_1997_2017_``type'_abbr' == 1) ;
			replace n_`j'crime2noemp`amt'``type'_first't_1997_2017 = 1 
				if (keep_1997_2017_``type'_abbr' == 1)
				& n_`j'crime2`type't_1997_2017 > 1 & n_`j'crime2`type't_1997_2017 != .
				& avg_`j'kidemp`amt'_1997_2017 < `empcut' & avg_`j'kidemp`amt'_1997_2017 != . ;
		} ;
		
		/* No crime, but employment */		
		foreach type in `list_emp' { ;
			gen 		n_`j'crimeno2emp`amt'``type'_first'_1997_2017= 0 
					if (keep_1997_2017_``type'_abbr' == 1) ;
			replace n_`j'crimeno2emp`amt'``type'_first'_1997_2017 = 1 
				if (keep_1997_2017_``type'_abbr' == 1)
				& n_`j'crime2`type'_1997_2017 == 0
				& avg_`j'kidemp`amt'_1997_2017 >= `empcut' & avg_`j'kidemp`amt'_1997_2017 != . ;
		} ;
		
		foreach type in `list_emp' { ;
			gen 		n_`j'crimeno2emp`amt'``type'_first't_1997_2017= 0 
					if (keep_1997_2017_``type'_abbr' == 1) ;
			replace n_`j'crimeno2emp`amt'``type'_first't_1997_2017 = 1 
				if (keep_1997_2017_``type'_abbr' == 1)
				& n_`j'crime2`type't_1997_2017 <= 1
				& avg_`j'kidemp`amt'_1997_2017 >= `empcut' & avg_`j'kidemp`amt'_1997_2017 != . ;
		} ;
		
		/* No crime, no employment */
		foreach type in `list_emp' { ;
			gen 		n_`j'crimeno2noe`amt'``type'_first'_1997_2017= 0 
					if (keep_1997_2017_``type'_abbr' == 1) ;
			replace n_`j'crimeno2noe`amt'``type'_first'_1997_2017 = 1 
				if (keep_1997_2017_``type'_abbr' == 1)
				& n_`j'crime2`type'_1997_2017 == 0
				& avg_`j'kidemp`amt'_1997_2017 < `empcut' & avg_`j'kidemp`amt'_1997_2017 != . ;
		} ;
		
		foreach type in `list_emp' { ;
			gen 		n_`j'crimeno2noe`amt'``type'_first't_1997_2017= 0 
					if (keep_1997_2017_``type'_abbr' == 1) ;
			replace n_`j'crimeno2noe`amt'``type'_first't_1997_2017 = 1 
				if (keep_1997_2017_``type'_abbr' == 1)
				& n_`j'crime2`type't_1997_2017 <= 1
				& avg_`j'kidemp`amt'_1997_2017 < `empcut' & avg_`j'kidemp`amt'_1997_2017 != . ;
		} ;
		
	} ; /* end amt */
			
} ; /* end j */



/* Look at thresholds joint with employment */
local thresh_list "char genr prtf prbg prfd vrob vcps dgsl" ;
local thresh_max = 10 ;

foreach stub in `thresh_list' {;

	forval i = 1(1)`thresh_max' { ;
		gen 	ct_crime2`stub'`i' = 0 if n_crime2`stub't_1997_2017 != . ;
		replace ct_crime2`stub'`i' = 1 if (n_crime2`stub't_1997_2017 > `i' & n_crime2`stub't_1997_2017 !=.) ;
	} ;
} ;
	
foreach stub in char genr { ;

	forval i = 1(1)`thresh_max' { ;
		gen 	ct_crime2`stub'`i'emp15 = 0 if ct_crime2`stub'`i' !=. & avg_kidemp15_1997_2017 != . ;
		replace ct_crime2`stub'`i'emp15 = 1 if ct_crime2`stub'`i' == 1 & avg_kidemp15_1997_2017 >= `empcut' ;
	} ;
	
	forval i = 1(1)`thresh_max' { ;
		gen 	ct_crime2`stub'`i'noemp15 = 0 if ct_crime2`stub'`i' !=. & avg_kidemp15_1997_2017 != . ;
		replace ct_crime2`stub'`i'noemp15 = 1 if ct_crime2`stub'`i' == 1 & avg_kidemp15_1997_2017 <  `empcut' ;
	} ;
	
	forval i = 1(1)`thresh_max' { ;
		gen 	ct_crimeno2`stub'`i'emp15 = 0 if ct_crime2`stub'`i' !=. & avg_kidemp15_1997_2017 != . ;
		replace ct_crimeno2`stub'`i'emp15 = 1 if ct_crime2`stub'`i' == 0 & avg_kidemp15_1997_2017 >= `empcut' ;
	} ;
	
	forval i = 1(1)`thresh_max' { ;
		gen 	ct_crimeno2`stub'`i'noemp15 = 0 if ct_crime2`stub'`i' !=. & avg_kidemp15_1997_2017 != . ;
		replace ct_crimeno2`stub'`i'noemp15 = 1 if ct_crime2`stub'`i' == 0 & avg_kidemp15_1997_2017 <  `empcut' ;
	} ;
	
} ;


summ n_crime* c_crime* n_1crime* n_2crime* n_3crime* n_4crime* n_yr* ct_* ;

gen byte n_post=(dobdd_run>0);
tab n_post, m;

foreach stub in any 2c 6cg 7cn { ;
	rename keep_1997_2017_`stub' tkeep_1997_2017_`stub' ;
} ;

drop 
	first_name fssr_middlen last_name from_prwora from_name idu middle_name
	combpmt*
	n_combpay_* n_sibpayall_* 
	n_hhearns1996_* n_hhmbayr1996_* n_hhcombpay1996_* n_inchh1996_* 
	n_hhearns_* n_hhmbayr_* n_hhcombpay_* n_inchh_* earn*
	days_in_* event* day_of_week* 
	keep_* i ;

foreach stub in any 2c 6cg 7cn { ;
	rename tkeep_1997_2017_`stub' keep_1997_2017_`stub' ;
} ;

save "`crime'\prep2\crime_prwora_prepforreg.dta", replace ;

capture log close;
